<?php
class Slog_View
{
	const EXTENSION = '.phtml';
	
	protected $_data;
	
	public function __construct()
	{
		$this->_data = array();
	}
	
	public function parse($file)
	{
		$fullpath = SLOG_DIR_APP . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $file . self::EXTENSION;
		if (is_file($fullpath)) {
			ob_start();
			require($fullpath);
			return ob_get_clean();
		} else {
			throw new Exception('Vista no existe: ' . $file);
		}
	}
	
	public function __get($name)
	{
		return isset($this->_data[$name]) ? $this->_data[$name] : null;
	}
	public function __set($name, $value)
	{
		$this->_data[$name] = $value;
	}
	
	public function __call($method, $params)
	{
		// 1.- convierte $method no_camel_case en $method CamelCase
		$CamelCaseMethod = ucfirst(preg_replace_callback('/(_[a-z]{1})/i', create_function(
			'$item',
			'return strtoupper(substr($item,1));'
		), $method));
		// 2.- se busca el helper: Slog_View_Helper_CamelCaseMethod
		$classname = 'Slog_View_Helper_' . $CamelCaseMethod;
		if (class_exists($classname)) {
			// 3.- se ejecuta run()
			$obj = new $classname();
			return $obj->run($params);
		}
		return null;
	}
}